package com.jhws.business.card.bo;

import com.jhws.business.card.service.ITblCardDimService;
import com.jhws.core.dataUpdate.bo.DataUpdateObject;
import com.jhws.core.dataUpdate.bo.IDataUpdateModule;
import com.jhws.core.dataUpdate.service.DataUpdateService;

import com.jhws.util.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class CardUpdateData implements IDataUpdateModule {

    static public String CARD_PERMS = "card_perms";
    private String [] name = {CARD_PERMS};

    @Resource
    DataUpdateService dataUpdateService;

    @Resource
    ITblCardDimService ICardService;

    @Override
    public String [] getModuleName() {
        return name;
    }

    /**
     * 获取该梯口机需要的普通用户卡的挂失数据
     * @param username      账号
     * @param timeStamp     通知写入的时间戳
     * @param module        有推送需求的模块名
     * @param moduleParams  模块专属参数
     * @return
     * @throws Exception
     */
    @Override
    public DataUpdateObject getUserModuleData(String username,String ownerCode, Long timeStamp, String module, Map<String, String> moduleParams) throws Exception {

        String IDMode = moduleParams.get("IDMode");
        List<TblCardPerms> list = ICardService.findTblCardsWithTimestamp(username, timeStamp, StringUtils.isNotEmpty(IDMode));
        List<CardPermitInfo> cardPermitList = new ArrayList<>();
        for (TblCardPerms perm : list) {
            TblCardDimBo cardDimBo = ICardService.findCardInfo(perm.getCardNo());

            CardPermitInfo permitInfo = new CardPermitInfo(
                    perm.getCardNo(),
                    cardDimBo.getOwnerName(),
                    cardDimBo.getRoomName(),
                    CardConstant.CARD_PERM_EXPIRE.equals(perm.getEnable())?CardConstant.CARD_PERM_BLACKLIST:perm.getEnable(),
                    perm.getTimeStamp()
            );

            cardPermitList.add(permitInfo);
        }
        return new DataUpdateObject(cardPermitList);
    }


    /**
     * 获取该小区超级卡的挂失数据
     * @param commCode
     * @param timeStamp      上次查询的时间戳
     * @param module         有增量和推送需求的模块名
     * @param moduleParams   模块专用参数
     * @return
     * @throws Exception
     */
    @Override
    public DataUpdateObject getLabelModuleData(String commCode, Long timeStamp, String module, Map<String,String> moduleParams) throws Exception {

        String whereForSuperCard = "where 1=1 and commCode = '"+commCode+"' and timestamp > '"+timeStamp+"' and state != '"+CardConstant.CARD_TYPE_NORMAL+"' and cardType = '"+CardConstant.CARD_TYPE_SUPPER+"'";
        List<TblCardDimBo> list = ICardService.findTblCardDimByWhere(whereForSuperCard);
        List<CardPermitInfo> retList = new ArrayList<>();
        for(TblCardDimBo card : list){
            String enable = "";
            if(CardConstant.CARD_STATUS_DISABLED.equals(card.getState()) || CardConstant.CARD_STATUS_EXPIRE.equals(card.getState()))
                enable = CardConstant.CARD_PERM_BLACKLIST;
            else
                enable = CardConstant.CARD_PERM_WHITELIST;

            CardPermitInfo permitInfo = new CardPermitInfo(
                    card.getCardNo(),
                    card.getOwnerName(),
                    "管理处",
                    enable,
                    card.getLastModifyTime().getTime()
            );
            retList.add(permitInfo);
        }
        return new DataUpdateObject(retList);
    }

    @Override
    @PostConstruct
    public void registerModule() {
        dataUpdateService.registerModule(this);
    }
}
